﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using System.IO;

namespace ExpreRec.Logic.Util
{
    public class BinaryFileHelper
    {
        public static CvMat GetMat2(string fileName)
        {
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);

            int rows, cols;
            rows = (int)br.ReadDouble();
            cols = (int)br.ReadDouble();

            CvMat m = new CvMat(rows, cols, MatrixType.F64C2);

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                {
                    m.DataArrayDouble[m.Width * 2 * i + j * 2] = br.ReadDouble();
                    m.DataArrayDouble[m.Width * 2 * i + j * 2 + 1] = br.ReadDouble();
                }

            br.Close();
            fs.Close();

            return m;
        }

        public static CvMat GetMat(string fileName)
        {
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);

            int rows, cols;
            rows = (int)br.ReadDouble();
            cols = (int)br.ReadDouble();

            CvMat m = new CvMat(rows, cols, MatrixType.F64C1);

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                {
                    m.DataArrayDouble[m.Width * i + j] = br.ReadDouble();
                }

            br.Close();
            fs.Close();

            return m;
        }

        public static void WriteMat2(CvMat m, string fileName)
        {
            FileStream fs = File.OpenWrite(fileName);
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write((double)m.Rows);
            bw.Write((double)m.Cols);

            for (int i = 0; i < m.Rows; i++)
                for (int j = 0; j < m.Cols; j++)
                {
                    bw.Write(m.DataArrayDouble[m.Width * 2 * i + j * 2]);
                    bw.Write(m.DataArrayDouble[m.Width * 2 * i + j * 2 + 1]);
                }

            bw.Close();
            fs.Close();
        }

        public static void WriteMat(CvMat m, string fileName)
        {
            FileStream fs = File.OpenWrite(fileName);
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write((double)m.Rows);
            bw.Write((double)m.Cols);

            for (int i = 0; i < m.Rows; i++)
                for (int j = 0; j < m.Cols; j++)
                    bw.Write(m.DataArrayDouble[m.Cols * i + j]);

            fs.Close();
        }

        public static void WriteData(double[] v, string fileName)
        {
            FileStream fs = File.OpenWrite(fileName); //Stream for output.//Pointer to all the element to save.           
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write((double)v.Length);
            bw.Write((double)1);//Save the dimensions of the matrix.

            for (int i = 0; i < v.Length; i++)
                bw.Write(v[i]);

            bw.Close();
            fs.Close();
        }

        public static double[] GetData(string fileName)
        {
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);

            int length = (int)br.ReadDouble();
            br.ReadDouble();

            double[] res = new double[length];

            for (int i = 0; i < length; i++)
            {
                res[i] = br.ReadDouble();
            }
            return res;
        }
    }
}
